#include "clientservicethread.h"

ClientServiceThread::ClientServiceThread(int socketID, int state, QObject *parent):
    QThread(parent),serviceState(state),socketID(socketID)
{
    //注册
    qRegisterMetaType<FreedDogStruct>("FreedDogStruct");
    qRegisterMetaType< QMap<qint16,qint8> >("QMap<qint16,qint8>");
    qRegisterMetaType<LcdSettingStruct>("LcdSettingStruct");
    qRegisterMetaType<SystemState>("SystemState");
    qRegisterMetaType< QList<LCDSTATUS> >("QList<LCDSTATUS>");
    qRegisterMetaType< QList<PanelInfoStruct> >("QList<PanelInfoStruct>");
    qRegisterMetaType< QList<quint16> >("QList<quint16>");




}

/*******************************************
  功能：析构函数，先等待200ms通知客户端服务器即将关闭，让客户端自我断开连接,
        然后关闭等待200ms以便程序正确关闭
  参数：
  返回值：
  ******************************************/
 ClientServiceThread::~ClientServiceThread()
 {
     if(this->isRunning())
     {
         this->wait(200);
         this->exit();
         delete csv;
     }

 }

/*******************************************
  功能：开始运行，一般调用start()之后，就会运行此函数
  参数：
  返回值：
  ******************************************/
void ClientServiceThread::run()
{

    csv = new ClientService(socketID, serviceState);
//    connect(csv,SIGNAL(destroyed()),this,SLOT(quit()));
//    connect(csv, SIGNAL(receSetLcdSettingsCmd(LcdSettingStruct)), this, SIGNAL(receSetLcdSettingsCmd(LcdSettingStruct)));
//    connect(csv, SIGNAL(receGetLcdStatusCmd()), this, SIGNAL(receGetLcdStateCmd()));

    connect(csv, SIGNAL(receFeedData(FreedDogStruct)),      this, SIGNAL(receFreedData(FreedDogStruct)));
    connect(csv, SIGNAL(serviceInfoSignal(QString,int)),    this, SIGNAL(serviceInfoSignal(QString,int)));
    connect(csv, SIGNAL(updateService(int,int)),            this, SIGNAL(updateService(int,int)));


    connect(csv, SIGNAL(disconnectSignal()),                this, SLOT(quit()));
    connect(csv, SIGNAL(receSetLcdIDListCmd(QMap<qint16,qint8>)),   this, SIGNAL(receSetLcdIDListCmd(QMap<qint16,qint8>)));
    connect(csv, SIGNAL(changeBriLv(LcdSettingStruct)),             this, SIGNAL(receChangeBriLv(LcdSettingStruct)));
    connect(csv, SIGNAL(changeLcdBright(qint16,QList<quint16>)),    this, SIGNAL(receChangeLcdBright(qint16,QList<quint16>)));
    connect(csv, SIGNAL(getLcdStatus()),                    this, SIGNAL(receGetLcdStatus()));

    connect(csv, SIGNAL(receLcdBackLightCtl(qint8,QList<quint16>)), this, SIGNAL(receLcdBackLightCtl(qint8,QList<quint16>)));

    connect(csv, SIGNAL(receQueryVideoStatus(qint16)),      this, SIGNAL(receQueryVideoStatus(qint16)));
    connect(csv, SIGNAL(receSlcdCollectState(QList<PanelInfoStruct>)), this, SIGNAL(receSlcdCollectState(QList<PanelInfoStruct>)));
    connect(csv, SIGNAL(getLcdSetting()),                   this, SIGNAL(getLcdSetting()));
    connect(csv, SIGNAL(updateConfig()),                    this, SIGNAL(updateConfig()));
    connect(csv, SIGNAL(checkLcdCmd(qint8)),                this, SIGNAL(receCheckLcdCmd(qint8)));

    connect(this, SIGNAL(sendSetLcdSettingStatus(qint8)),   csv, SLOT(sendSetLcdSettingStatus(qint8)));
    connect(this, SIGNAL(sendSetLcdTraValueStatus(qint8)),  csv, SLOT(sendSetLcdTraValueStatus(qint8)));

    connect(this, SIGNAL(sendBrihgtness(qint16)),           csv, SLOT(sendEnvirBrightness(qint16)));
    connect(this, SIGNAL(serviceClose()),                   csv, SLOT(sendServiceClose()));

//    connect(this, SIGNAL(setLcdTraValueState(qint8)), csv, SLOT(sendSetLcdSettingStatus(qint8)));
    connect(this, SIGNAL(sendLcdSetting(LcdSettingStruct)), csv, SLOT(sendLcdSetting(LcdSettingStruct)));
//    connect(this, SIGNAL(sendBackLightStatus(qint8)),       csv, SLOT(sendBackLightStatus(qint8)));
    connect(this, SIGNAL(sendVideoStatus(QList<LCDSTATUS>,bool)), csv, SLOT(sendVideoStatus(QList<LCDSTATUS>,bool)));
    connect(this, SIGNAL(receSystemState(SystemState)),     csv, SLOT(sendSystemState(SystemState)));

    connect(this, SIGNAL(lcdAnswerStatus(QList<LCDSTATUS>)), csv, SLOT(sendLcdAnswerStatus(QList<LCDSTATUS>)));

    exec();
}

